/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function loadXMLString(txt) {
    if (window.DOMParser) {
        parser=new DOMParser();
        xmlDoc=parser.parseFromString(txt,"text/xml");
    } else {// Internet Explorer
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async="false";
        xmlDoc.loadXML(txt);
    }
    return xmlDoc;
}

function insertField(parent_tag, tag_name, color) {
    temp=parent_tag.getElementsByTagName(tag_name);
    if (!temp || !temp[0].hasChildNodes()) {
        return "<td> </td>";
    } else {
        if (color) {
            return "<td color="+color+"><b>" + temp[0].firstChild.nodeValue + "</b></td>";
        } else {
            return "<td><b>" + temp[0].firstChild.nodeValue + "</b></td>";
        }
    }
}

function newHttpRequest() {
    if (window.XMLHttpRequest) {
     // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else {
      // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlhttp;
}

function ajaxCall(srvr_request, srvr_callback, init_data) {
    xmlhttp=newHttpRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            srvr_callback(xmlhttp.responseText, init_data);
        }
    }

    xmlhttp.open("POST",srvr_request,true);
    xmlhttp.send();
}

function addSelectOptions(select_name, option_value, option_text) {
    var select=document.getElementById(select_name);
    var optn = new Option(option_value+" - "+option_text,option_value);
    select.options[select.options.length]=optn;
}

function removeAllSelectOptions(select_name) {
    var select=document.getElementById(select_name);
    select.options.length = 0;
}

function selectOptionValue(select_name, option_value) {
    var select=document.getElementById(select_name);
    for(var i=0; i<select.options.length; i++){
        if(select.options[i].value == option_value){
            select.selectedIndex = i;
            return;
        }
    }
}

function setCookie(c_name,value,expiretime)
{
    if (getCookie(c_name)) {
        deleteCookie(c_name);
    }
    var exdate=new Date();
    exdate.setTime(exdate.getTime()+expiretime);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiretime==null) ? "" : ";expires="+exdate.toUTCString());
}

function deleteCookie(c_name)
{
    var exdate=new Date();
    exdate.setTime(exdate.getDate());
    document.cookie=c_name+ "=" + escape(0) + ";expires="+exdate.toUTCString();
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        }
      }
    return "";
}

/*----------------------------------------------------------------------------
    f_sort: Performs a client side sort of the contents of a HTML Table.

    args:   ao_table, The object reference to the table to be sorted.
            ai_sortcol, The zero based column number to be sorted.
            ab_header, Bool to indicate if the table have a header
                row to be ignored.

    vars:   lastcol, used to store the last column sorted.
            lastseq, used to store the sequence the last column was sorted by.
----------------------------------------------------------------------------*/
var lastcol, lastseq;
function f_sort( ao_table, ai_sortcol, ab_header )
{
    var ir, ic, is, ii, id;

    ir = ao_table.rows.length;
    if( ir < 1 ) return;

    ic = ao_table.rows[1].cells.length;
    // if we have a header row, ignore the first row
    if( ab_header == true ) is=1; else is=0;

    // take a copy of the data to shuffle in memory
    var row_data = new Array( ir );
    ii=0;
    for( i=is; i < ir; i++ )
    {
        var col_data = new Array( ic );
        for( j=0; j < ic; j++ )
        {
            col_data[j] = ao_table.rows[i].cells[j].innerText;
        }
        row_data[ ii++ ] = col_data;
    }

    // sort the data
    var bswap = false;
    var row1, row2;

    if( ai_sortcol != lastcol )
        lastseq = 'A';
    else
    {
        //if( lastseq == 'A' ) lastseq = 'D'; else lastseq = 'A';
        lastseq = 'A';
    }

    // if we have a header row we have one less row to sort
    if( ab_header == true ) id=ir-1; else id=ir;
    for( i=0; i < id; i++ )
    {
        bswap = false;
        for( j=0; j < id - 1; j++ )
        {
            // test the current value + the next and
            // swap if required.
            row1 = row_data[j];
            row2 = row_data[j+1];
            if( lastseq == "A" )
            {
                if( row1[ ai_sortcol ] > row2[ ai_sortcol ] )
                {
                    row_data[j+1] = row1;
                    row_data[j] = row2;
                    bswap=true;
                }
            }
            else
            {
                if( row1[ ai_sortcol ] < row2[ ai_sortcol ] )
                {
                    row_data[j+1] = row1;
                    row_data[j] = row2;
                    bswap=true;
                }
            }
        }
        if( bswap == false ) break;
    }

    // load the data back into the table
    ii = is;
    for( i=0; i < id; i++ )
    {
        row1 = row_data[ i ];
        for( j=0; j < ic; j++ )
        {
            ao_table.rows[ii].cells[j].innerText = row1[j];
        }
        ii++;
    }
    lastcol = ai_sortcol;
}

